home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_gen / euphor14.zip / SYNCOLOR.E < prev    next >
Text File  |  1994-07-19  |  4KB  |  169 lines

  1. --              Syntax Color
  2. -- These routines are used to display Euphoria program lines
  3. -- in multiple colors. The editor (ed.ex) and the pretty printer (eprint.ex)
  4. -- both include this file.
  5.  
  6. -- this file assumes that the following symbols have already
  7. -- been defined:
  8. --      SCREEN        - file/device number to write output to
  9. --       NORMAL_COLOR - colors of various syntax classes
  10. --      COMMENT_COLOR
  11. --      KEYWORD_COLOR
  12. --      BUILTIN_COLOR
  13. --       STRING_COLOR
  14. --      BRACKET_COLOR
  15.  
  16. include keywords.e
  17.  
  18. -- character classes
  19. constant DIGIT = 1,
  20.      OTHER = 2,
  21.      LETTER  = 3,
  22.      BRACKET = 4,
  23.      QUOTE   = 5,
  24.      DASH = 6,
  25.      WHITE_SPACE = 7,
  26.      NEW_LINE = 8
  27.  
  28. sequence char_class
  29.  
  30. global procedure init_class()
  31. -- set up character classes for easier line scanning
  32. -- (assume no 0 char)
  33.     char_class = repeat(OTHER, 255)
  34.  
  35.     char_class['a'..'z'] = LETTER
  36.     char_class['A'..'Z'] = LETTER
  37.     char_class['_'] = LETTER
  38.     char_class['0'..'9'] = DIGIT
  39.     char_class['['] = BRACKET
  40.     char_class[']'] = BRACKET
  41.     char_class['('] = BRACKET
  42.     char_class[')'] = BRACKET
  43.     char_class['{'] = BRACKET
  44.     char_class['}'] = BRACKET
  45.     char_class['\''] = QUOTE
  46.     char_class['"'] = QUOTE
  47.     char_class[' '] = WHITE_SPACE
  48.     char_class['\t'] = WHITE_SPACE
  49.     char_class['\n'] = NEW_LINE
  50.     char_class['-'] = DASH
  51. end procedure
  52.  
  53. sequence line  -- the line being processed
  54. integer seg_start, seg_end -- start and end of current segment of line
  55. integer color  -- the current color
  56.  
  57. procedure flush(integer new_color)
  58. -- if the color is changing, write out the current segment
  59.     if new_color != color then
  60.     if color != -1 then
  61.         text_color(color)
  62.         puts(SCREEN, line[seg_start..seg_end])
  63.         seg_start = seg_end + 1
  64.     end if
  65.     color = new_color
  66.     end if
  67. end procedure
  68.  
  69. global procedure DisplayColorLine(sequence pline)
  70. -- Display a '\n'-terminated line with colors identifying the various
  71. -- parts of the Euphoria language.
  72. -- Each screen write has a lot of overhead, so we try to minimize
  73. -- the number of them by collecting consecutive characters of the
  74. -- same color into a 'segment' seg_start..seg_end.
  75.     integer class, last, i, c, bracket_level
  76.     sequence word
  77.  
  78.     line = pline
  79.     color = -1 -- initially undefined
  80.     bracket_level = 0
  81.     seg_start = 1
  82.     seg_end = 0
  83.  
  84.     while 1 do
  85.     c = line[seg_end+1]
  86.     class = char_class[c]
  87.  
  88.     if class = WHITE_SPACE then
  89.         seg_end = seg_end + 1 -- continue with same color
  90.  
  91.     elsif class = LETTER then
  92.         last = length(line)-1
  93.         for j = seg_end + 2 to last do
  94.         c = line[j]
  95.         class = char_class[c]
  96.         if class != LETTER then
  97.             if class != DIGIT then
  98.             last = j - 1
  99.             exit
  100.             end if
  101.         end if
  102.         end for
  103.         word = line[seg_end+1..last]
  104.         if find(word, keywords) then
  105.         flush(KEYWORD_COLOR)
  106.         elsif find(word, builtins) then
  107.         flush(BUILTIN_COLOR)
  108.         else
  109.         flush(NORMAL_COLOR)
  110.         end if
  111.         seg_end = last
  112.  
  113.     elsif class <= OTHER then -- DIGIT too
  114.         flush(NORMAL_COLOR)
  115.         seg_end = seg_end + 1
  116.  
  117.     elsif class = BRACKET then
  118.         if find(c, "([{") then
  119.         bracket_level = bracket_level + 1
  120.         end if
  121.         if bracket_level >= 1 and
  122.            bracket_level <= length(BRACKET_COLOR) then
  123.         flush(BRACKET_COLOR[bracket_level])
  124.         else
  125.         flush(NORMAL_COLOR)
  126.         end if
  127.         if find(c, ")]}") then
  128.         bracket_level = bracket_level - 1
  129.         end if
  130.         seg_end = seg_end + 1
  131.  
  132.     elsif class = NEW_LINE then
  133.         exit  -- end of line
  134.     
  135.     elsif class = DASH then
  136.         if line[seg_end+2] = '-' then
  137.         flush(COMMENT_COLOR)
  138.         seg_end = length(line)-1
  139.         exit
  140.         end if
  141.         flush(NORMAL_COLOR)
  142.         seg_end = seg_end + 1
  143.  
  144.     else  -- QUOTE
  145.         i = seg_end + 2
  146.         while i < length(line) do
  147.         if line[i] = c then
  148.             i = i + 1
  149.             exit
  150.         elsif line[i] = '\\' then
  151.             if i < length(line)-1 then
  152.             i = i + 1 -- ignore escaped char
  153.             end if
  154.         end if
  155.         i = i + 1
  156.         end while
  157.         flush(STRING_COLOR)
  158.         seg_end = i - 1
  159.     end if
  160.     end while
  161.  
  162.     if color != -1 then
  163.     text_color(color)
  164.     end if
  165.     puts(SCREEN, line[seg_start..seg_end])
  166. end procedure
  167.  
  168.  
  169.